/**
 * std::atomic
 */

#include <atomic>
#include <chrono>
#include <iostream>
#include <mutex>
#include <thread>

using namespace std::chrono;

std::atomic<int> a = 0;
int b = 0;
int c = 0;
std::mutex m;

void thread_task1() {
    for (int i = 0; i < 100000; ++i) {
        ++a;
    }
}

void thread_task2() {
    for (int i = 0; i < 100000; ++i) {
        ++b;
    }
}

void thread_task3() {
    for (int i = 0; i < 100000; ++i) {
        m.lock();
        ++c;
        m.unlock();
    }
}

using Func = std::function<void()>;
void test_thread(Func func) {
    std::cout << "==========" << std::endl;
    auto begin_time = system_clock::now();
    std::thread t1(func);
    std::thread t2(func);
    t1.join();
    t2.join();
    auto duration = duration_cast<milliseconds>(system_clock::now() - begin_time);
    std::cout << "timecost: " << (double) duration.count() << " ms" << std::endl;
}

int main() {
    test_thread(thread_task1);
    std::cout << "thread_task1: " << a << std::endl;

    test_thread(thread_task2);
    std::cout << "thread_task2: " << b << std::endl;

    test_thread(thread_task3);
    std::cout << "thread_task3: " << c << std::endl;
    return 0;
}